package com.blockchain.securewallet.view;

import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockchain.securewallet.utils.Logger;

/**
 * Created by Lopez on 2018/5/25.
 */
public class SelectorGroupView extends ConstraintLayout {

    private static final float ALPHA_CLICK = 0.1f;
    private static final float ALPHA_NORMAL = 1.0f;

    public SelectorGroupView(Context context) {
        super(context);
        this.setOnTouchListener(onTouchListener);
    }

    public SelectorGroupView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(onTouchListener);
    }

    public SelectorGroupView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOnTouchListener(onTouchListener);
    }


    public OnTouchListener onTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            SelectorGroupView sgv = (SelectorGroupView) v;
            View view1 = sgv.getChildAt(0);
            View view2 = sgv.getChildAt(1);
            View view3 = null;
            View view4 = null;
            View view5 = null;

            int count = sgv.getChildCount();
            //Logger.ds("当前ViewGroup内共有" + count + "个子元素。");
            /*for (int i = 0; i < count; i++) {
                View child = sgv.getChildAt(i);
                Class<? extends View> aClass = child.getClass();
                Logger.ds(i + ": " + aClass.getName());
            }*/

            switch (count) {
                case 5:
                    view5 = sgv.getChildAt(4);
                case 4:
                    view4 = sgv.getChildAt(3);
                case 3:
                    view3 = sgv.getChildAt(2);
                    break;
            }

            ImageView iv = null;
            ImageView iv2 = null;
            TextView tv = null;
            TextView tv2 = null;
            TextView tv3 = null;
            ImageButton ivb = null;

            //1.主页侧滑菜单、2.钱包信息界面下面的列表@activity_wallet_info.xml、3.钱包信息界面上面的余额、4.主页货币界面 
            //boolean is_type_menu = (view1 instanceof ImageView) && (view2 instanceof TextView);
            //boolean is_type_wallet_list = (view1 instanceof TextView) && (view2 instanceof ImageButton);
            //boolean is_type_wallet_info = (view1 instanceof TextView) && (view2 instanceof TextView) && ((view3 != null) && view3 instanceof ImageButton);
            //boolean is_type_coin_list = (view1 instanceof ImageView) && (view2 instanceof TextView) && ((view3 != null) && view3 instanceof ImageView) && (((view4 != null) && view4 instanceof TextView) && ((view5 != null) && view5 instanceof TextView));

            boolean b1 = (view1 instanceof ImageView);
            boolean b2 = (view2 instanceof TextView);
            boolean b3 = (view3 != null) && (view3 instanceof ImageView);
            boolean b4 = ((view4 != null) && view4 instanceof TextView);
            boolean b5 = ((view5 != null) && view5 instanceof TextView);
            //Logger.ds("b1:" + b1 + "\t" + "b2:" + b2 + "\t" + "b3:" + b3 + "\t" + "b4:" + b4 + "\t" + "b5:" + b5);

            if ((b1) && (b2) && (b3) && (b4) && (b5)) {
                iv = (ImageView) view1;
                tv = (TextView) view2;
                iv2 = (ImageView) view3;
                tv2 = (TextView) view4;
                tv3 = (TextView) view5;
                Logger.ds("comein type coin");

            } else if ((view1 instanceof ImageView) && (view2 instanceof TextView)) {
                iv = (ImageView) view1;
                tv = (TextView) view2;
                Logger.ds("comein type menu");

            } else if ((view1 instanceof TextView) && (view2 instanceof ImageButton)) {
                tv = (TextView) view1;
                ivb = (ImageButton) view2;
                Logger.ds("comein type wallet_list");

            } else if ((view1 instanceof TextView) && (view2 instanceof TextView) && ((view3 != null) && view3 instanceof ImageButton)) {
                tv = (TextView) view1;
                tv2 = (TextView) view2;
                ivb = (ImageButton) view3;

                Logger.ds("comein type wallet_info");
            }

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    setAlphaClick(iv);
                    setAlphaClick(iv2);
                    setAlphaClick(tv);
                    setAlphaClick(tv2);
                    setAlphaClick(tv3);
                    setAlphaClick(ivb);
                    break;
                case MotionEvent.ACTION_UP:
                    mPerformClick();
                case MotionEvent.ACTION_CANCEL:
                    setAlphaNormal(iv);
                    setAlphaNormal(iv2);
                    setAlphaNormal(tv);
                    setAlphaNormal(tv2);
                    setAlphaNormal(tv3);
                    setAlphaNormal(ivb);
                    break;
            }
            return true;  //如为false，执行ACTION_DOWN后不再往下执行
        }
    };

    private void mPerformClick() {
        SelectorGroupView.this.performClick();
    }

    private void setAlphaClick(View view) {
        if (view != null) {
            view.setAlpha(ALPHA_CLICK);
        }
    }

    private void setAlphaNormal(View view) {
        if (view != null) {
            view.setAlpha(ALPHA_NORMAL);
        }
    }

}
